﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using log4net;
using log4net.Config;
using Ninject.Modules;
using Quartz;
using Topshelf;
using Topshelf.Ninject;
using Topshelf.Quartz;
using Topshelf.Quartz.Ninject;
using Dln.Handler;
using Dln.Model;
using Dln.Common;
using Dln.Common.Helper;


namespace AutoRun
{
    public class SearchWebserviceData : IJob
    {
        private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public void Execute(IJobExecutionContext context)
        {
            log4net.Config.XmlConfigurator.Configure();
            string Key = "Laf5JVhF/H36iMGyB9dXpZq2j95klaqOaFlPqz4VwHuaA6oxp6JYEw==";
            string UnicodeKey = Key.Replace("+", "%2B");

            IList<WebServiceData> webServiceDataist = new List<WebServiceData>();
            WebServiceData model = null;
            
            //昨日入院人数
            object[] arge = new object[1];
            arge[0] = Key;
            var url = CommonHelper.GetAppSettings("DlnService1");
            object getCurrInHospNum = WebServiceHelper.InvokeWebService(url, "GetCurrDayInHospNum", arge);
            if (getCurrInHospNum != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetCurrDayInHospNum";
                model.ServiceData = getCurrInHospNum.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //昨日出院人数
            object getCurrOutHospNum = WebServiceHelper.InvokeWebService(url, "GetCurrOutHospNum", arge);
            if (getCurrOutHospNum != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetCurrOutHospNum";
                model.ServiceData = getCurrOutHospNum.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //昨日退院人数
            object getCurrLeaveHospNum = WebServiceHelper.InvokeWebService(url, "GetCurrLeaveHospNum", arge);
            if (getCurrLeaveHospNum != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetCurrLeaveHospNum";
                model.ServiceData = getCurrLeaveHospNum.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //今日在院退院人数
            object getCurrInHospNums = WebServiceHelper.InvokeWebService(url, "GetCurrInHospNum", arge);
            if (getCurrInHospNums != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetCurrInHospNum";
                model.ServiceData = getCurrInHospNums.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //昨日危重患者人数
            object getCurrCriticallyIllInHospNum = WebServiceHelper.InvokeWebService(url,
                "GetCurrCriticallyIllInHospNum", arge);
            if (getCurrCriticallyIllInHospNum != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetCurrCriticallyIllInHospNum";
                model.ServiceData = getCurrCriticallyIllInHospNum.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //昨日业务收入
            object getYesterdayAmount = WebServiceHelper.InvokeWebService(url, "GetYesterdayAmount", arge);
            if (getYesterdayAmount != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetYesterdayAmount";
                model.ServiceData = getYesterdayAmount.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            //昨日门诊人次
            object getYesterdayOutNum = WebServiceHelper.InvokeWebService(url, "GetYesterdayOutNum", arge);
            if (getYesterdayOutNum != null)
            {
                model = new WebServiceData();
                model.ServiceName = "GetYesterdayOutNum";
                model.ServiceData = getYesterdayOutNum.ToString();
                model.SearchDate = DateTime.Now.Date;
                webServiceDataist.Add(model);
            }

            if (webServiceDataist.Count > 0)
            {
                foreach (var item in webServiceDataist)
                {
                    var result = SqlHelper.Context.Insert<WebServiceData>(item);
                    if (result > 0)
                    {
                        Log.InfoFormat(item.ServiceName + "Success:" + item.ServiceData);
                    }
                }
            }

        }
    }
}
